package com.module.base.adapter.drag;

import android.graphics.Canvas;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

/**
 * Description :
 *
 * @author bsnl_yanxuwen
 * @date 2020/1/1310:58
 * RecycleView的拖动排序
 */
public class ItemDragCallback extends ItemTouchHelper.Callback {
    private DragAdapter mAdapter;
    private boolean mEdit;

    public ItemDragCallback(DragAdapter mAdapter) {
        this.mAdapter = mAdapter;
    }

    public interface Listener {

        public abstract void onChildDrawOver(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive);
    }

    public Listener listener;

    public void setListener(Listener listener) {
        this.listener = listener;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        DragAdapter adapter = (DragAdapter) recyclerView.getAdapter();
        mEdit = adapter.isEdit;
        //是否是编辑状态
        if (!mEdit) {
            return 0;
        }
        int position = viewHolder.getLayoutPosition();
        //item不用交换&
        if (adapter.isNotDragPosition(position) ) {
            return 0;
        }
        int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        int fromPosition = viewHolder.getAdapterPosition();   //拖动的position
        int toPosition = target.getAdapterPosition();     //释放的position
        int position = viewHolder.getLayoutPosition();
        DragAdapter adapter = (DragAdapter) recyclerView.getAdapter();
        //item不用交换
        if (adapter.isNotDragPosition(toPosition)) {
            return false;
        }
        mAdapter.itemMove(fromPosition, toPosition);
        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }


    @Override
    public void onChildDrawOver(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDrawOver(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        if (listener != null) {
            listener.onChildDrawOver(recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
        }
        if (dX != 0 && dY != 0 || isCurrentlyActive) {
            DragAdapter adapter = (DragAdapter) recyclerView.getAdapter();
            mEdit = adapter.isEdit;
        }
    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
            //长按时调用
            if (mAdapter == null) return;
            mAdapter.onStarDrag(viewHolder);
        }
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
//        super.clearView(recyclerView, viewHolder);
        //松手时调用
        if (mAdapter == null) return;
        mAdapter.onEndDrag(viewHolder);
    }
}
